package org.starapp.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

public class SecurityUtils {
	public static String md5(String str){
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i & 0xff));
			}
			return buf.toString(); // 32位
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}
	public static byte[] getSHAbytes(byte[] obj) {
		MessageDigest sha = null;
		try {
			sha = MessageDigest.getInstance("SHA");
			sha.update(obj);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return sha.digest();
	}

	public static byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
		if (publicKey != null) {
			try {
				Cipher cipher = Cipher.getInstance("RSA");
				cipher.init(Cipher.ENCRYPT_MODE, publicKey);
				return cipher.doFinal(obj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
		if (privateKey != null) {
			try {
				Cipher cipher = Cipher.getInstance("RSA");
				cipher.init(Cipher.DECRYPT_MODE, privateKey);
				return cipher.doFinal(obj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static void generateRSA(String publicKeyFilePath,
			String privateKeyFilePath) {
		try {
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");// 创建‘密匙对’生成器
			kpg.initialize(1024); // 指定密匙长度（取值范围：512～2048）
			KeyPair kp = kpg.genKeyPair(); // 生成‘密匙对’，其中包含着一个公匙和一个私匙的信息
			RSAPublicKey public_key = (RSAPublicKey) kp.getPublic(); // 获得公匙
			RSAPrivateKey private_key = (RSAPrivateKey) kp.getPrivate(); // 获得私匙

			/** 保存公匙 **/
			FileOutputStream public_file_out = new FileOutputStream(
					publicKeyFilePath);
			ObjectOutputStream public_object_out = new ObjectOutputStream(
					public_file_out);
			public_object_out.writeObject(public_key);

			/** 保存私匙 **/
			FileOutputStream private_file_out = new FileOutputStream(
					privateKeyFilePath);//
			ObjectOutputStream private_object_out = new ObjectOutputStream(
					private_file_out);
			private_object_out.writeObject(private_key);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static RSAPublicKey getPublicKeyFromFile(String publicKeyPath) {
		try {
			FileInputStream public_file_in = new FileInputStream(publicKeyPath);
			ObjectInputStream public_object_in = new ObjectInputStream(
					public_file_in);
			RSAPublicKey key = (RSAPublicKey) public_object_in.readObject();
			return key;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static RSAPrivateKey getPrivateKeyFromFile(String privateKeyPath) {
		try {
			FileInputStream private_file_in = new FileInputStream(privateKeyPath);// 保存私匙
			ObjectInputStream private_object_in = new ObjectInputStream(private_file_in);
			RSAPrivateKey key = (RSAPrivateKey) private_object_in.readObject();
			return key;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
